package me.xinping.libs.views

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.support.annotation.IdRes
import android.support.v4.content.ContextCompat
import android.support.v7.widget.Toolbar
import android.util.AttributeSet
import kotlinx.android.synthetic.main.view_title_toolbar.view.*

/**
 * common title view.
 * <p>
 * Date 2018/12/4
 * @author fanxinping_cd@keruyun.com
 */
class CommonTitleView : Toolbar {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)

    init {
        inflate(context, R.layout.view_title_toolbar, this)
        initView()
    }

    private fun initView() {
        //default back listener
        tv_left.setOnClickListener {
            context.let {
                if (context is Activity) {
                    (context as Activity).onBackPressed()
                }
            }
        }
    }

    fun setCenterTitle(strId: Int?) {
        tv_center.text = strId?.let { if (it > 0) context.getString(it) else "" }
    }

    fun setCenterTitle(title: String) {
        tv_center.text = title
    }

    @SuppressLint("ResourceType")
    fun setCenterTitleColor(@IdRes colorId: Int) {
        tv_center.setTextColor(ContextCompat.getColor(context, colorId))
    }

    fun setLeftText(strId: Int?) = setLeftText(strId, 0)

    fun setLeftImg(iconId: Int) = setLeftText(0, iconId)

    fun setLeftText(strId: Int?, iconId: Int?) {
        tv_left.text = strId?.let { if (it > 0) context.getString(it) else "" }
        val leftDrawable = iconId?.let { if (it > 0) ContextCompat.getDrawable(context, it) else null }
        tv_left.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null)
    }

    fun setLeftDrawable(drawable: Drawable?) {
        drawable?.let {
            tv_left.text = ""
            tv_left.setCompoundDrawablesWithIntrinsicBounds(it, null, null, null)
        }
    }

    fun setRightText(strId: Int?) = setRightText(strId, 0)

    fun setRightImg(iconId: Int?) = setRightText(0, iconId)

    fun setRightText(strId: Int?, iconId: Int?) {
        tv_right.text = strId?.let { if (it > 0) context.getString(it) else "" }
        val leftDrawable = iconId?.let { if (it > 0) ContextCompat.getDrawable(context, it) else null }
        tv_right.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null)
    }

    fun setRightDrawable(drawable: Drawable?) {
        drawable?.let {
            tv_right.text = ""
            tv_right.setCompoundDrawablesWithIntrinsicBounds(null, null, it, null)
        }
    }


    fun setOnLeftClickListener(listener: OnClickListener) = tv_left.setOnClickListener(listener)

    fun setOnLeftClickListener(listener: () -> Unit) = tv_left.apply { setOnClickListener { listener.invoke() } }

    fun setOnRightClickListener(listener: OnClickListener) = tv_right.setOnClickListener(listener)

    fun setOnRightClickListener(listener: () -> Unit) = tv_right.apply { setOnClickListener { listener.invoke() } }
}